<template>
	<view class="container">
	

		<uni-section title="提示" type="line">
			<view class="example">
				为了保证您的数据安全，在注销账户 24 小时之后，我们将停止为您提供产品或服务，并依据您的要求，删除您的个人信息，但法律法规另有规定的除外。
				<!-- 展示不同的排列方式 -->
				<uni-forms >
					
					<button type="primary" @click="deleteUser()">确定注销</button>
				</uni-forms>
			</view>
		</uni-section>

		
		
		
		
	</view>
</template>
<script>
	import store from '@/store/index.js';//需要引入store
	import constant from '@/store/constant.js'
	const baseUrl=constant.baseUrl;
	const baseImageUrl=constant.baseImageUrl;
	
	
	import md5 from 'js-md5';
	export default {
		computed:{
			errorMessage(){
				return store.state.user.errorMessage
			}
			
		},
		onLoad(option) {
			console.log("1111111111111111");
			console.log(JSON.stringify(option))
			
			if(option.userId!=undefined){
				this.baseData.userId=option.userId;
				this.baseData.nickName=option.nickName;
				this.baseData.introduction=option.introduction;
				this.baseData.pwd=option.pwd;
			}
		
		
		},
		data() {
			return {
				imageUrl:baseImageUrl,
				// 基础表单数据
				baseData: {
					userId:'',
					nickName: '',
					age: '',
					introduction: '',
					sex: 2,
					birthday: '',
					pwd:'',
					portrait:"",
					
				},
				basePwd:{
					oldpwd:'',
					pwd:'',
					repwd:''
				},
				// 表单数据
				alignmentFormData: {
					name: '',
					age: '',
				},
				// 单选数据源
				sexs: [{
					text: '男',
					value: 1
				}, {
					text: '女',
					value: 2
				}, {
					text: '保密',
					value: 0
				}],
				
				// 分段器数据
				current: 0,
				items: ['左对齐', '顶部对齐'],
				// 校验表单数据
				valiFormData: {
					name: '',
					age: '',
					introduction: '',
				},
			
				// 自定义表单数据
				customFormData: {
					name: '',
					age: '',
					hobby: []
				},
			
			
			}
		},
	
		
		onReady() {
			
		},
		methods: {
			selectPortrait(){
				let self=this;
				uni.chooseImage({
					crop:{"width":200,"height":200},
					count: 1,
					success: (res) => {
						
						uni.uploadFile({
							url: baseUrl+'/api/upload/uploadFile', 
							filePath: res.tempFilePaths[0],
							name: 'file',
							// formData: {
							// 	'user': 'test'
							// },
							success: (result) => {
								
								let data =JSON.parse(result.data);
								console.log(data)
								if(data.code==0){
									console.log(data.result.url)
									self.$data.baseData.portrait=data.result.url;
								}
								
							}
						});
					}
				})
			},
			onClickItem(e) {
				console.log(e);
				this.current = e.currentIndex
			},
			
			submit() {
				
				
				let data={
					"userId":this.$data.baseData.userId,
					"nickName":this.$data.baseData.nickName,
					"age":this.$data.baseData.age,
					"introduction":this.$data.baseData.introduction,
					"sex":this.$data.baseData.sex,
					"birthday":this.$data.baseData.birthday,
					"portrait":this.$data.baseData.portrait,
					
					
				}
				store.dispatch('editUserData',data).then(()=>{
					uni.showToast({
						icon: 'none',
						title: "信息保存成功" 
					})
					// uni.navigateTo({
					// 	url:"/pages/user/userInfo"
					// })
				});
			},
			deleteUser(){
				var userId=this.$data.baseData.userId;
				uni.showModal({
					title: '提示',
					content: '确定要删除用户数据吗',
					success: function (res) {
						if (res.confirm) {
							store.dispatch('deleteUserData',userId).then(()=>{
								
								uni.removeStorageSync('authInfo');
								uni.navigateTo({
									url:"/pages/login/login"
								})
							
								
								
							});
						} else if (res.cancel) {
							
						}
					}
				});
			},
			editPwd(){
				
				
				let self=this;
				if(this.$data.basePwd.oldpwd.length>0&&this.$data.basePwd.pwd.length>0&&this.$data.basePwd.repwd.length>0){
					
					      
					if(this.$data.basePwd.pwd==this.$data.basePwd.repwd){
						const md5Value = md5(this.$data.basePwd.oldpwd);
						if(md5Value==this.$data.baseData.pwd){
							let data={
								"userId":this.$data.baseData.userId,
								"pwd":this.$data.basePwd.pwd,
								"oldPwd":this.$data.basePwd.oldpwd,
								
							}
							store.dispatch('editPwdData',data).then(()=>{
								
								uni.showToast({
									icon: 'none',
									title: "密码修改成功" 
								})
							
								
								
							});
						}else{
							uni.showToast({
								icon: 'none',
								title: "旧密码不正确" 
							})
						}
						
					}else{
						uni.showToast({
							icon: 'none',
							title: "新密码和确认密码不一致" 
						})
					}
				}else{
					uni.showToast({
						icon: 'none',
						title: "相关的密码字段不可以为空" 
					})
				}
				
			},
			logout(){
				uni.removeStorageSync('authInfo');
				uni.navigateTo({
					url:"/pages/login/login"
				})
			}
		}
	}
</script>
<style lang="scss">

	.example {
		padding: 15px;
		background-color: #fff;
	}

	.segmented-control {
		margin-bottom: 15px;
	}

	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		margin-left: 10px;
	}
</style>